package ProjectManagement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
@WebServlet(name = "TLoginServlet",value = "/ProjectManagement/TLoginServlet.do")
public class LoginServlet extends HttpServlet {
    private static final long seriaVersionUID=1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession();
        req.setCharacterEncoding("utf-8");//解决乱码问题
        String username=req.getParameter("username").trim();//获取username并去掉空格
        String password=req.getParameter("password").trim();//获取password并去掉空格
        String imageValue=req.getParameter("imageValue");
        String remember=req.getParameter("remember");
        req.setAttribute("username", username);
        req.setAttribute("password", password);
        req.setAttribute("imageValue", imageValue);
        String sRand=(String) session.getAttribute("sRand");
        if(StringUtil.isEmpty(username)||StringUtil.isEmpty(password)){
            req.getRequestDispatcher("TeacherLogin.jsp").forward(req, resp);
            return;
        }
        if(StringUtil.isEmpty(imageValue)){
            req.setAttribute("error", "请输入验证码！");
            req.getRequestDispatcher("TeacherLogin.jsp").forward(req,resp);
            return;
        }else if(!imageValue.equals(sRand)){
            req.setAttribute("error", "验证码错误！");
            req.getRequestDispatcher("TeacherLogin.jsp").forward(req,resp);
            return;
        }
        Teacher teacher=new Teacher(username,password);
        TeacherDaoImpl usersdao=new TeacherDaoImpl();
        session.setAttribute("username", username);
        if(usersdao.isLogin(teacher)){
            if("remember-me".equals(remember)){
                rememberMe(username,password,resp);
            }
            resp.sendRedirect("main.jsp");
        }
        else{
            req.setAttribute("error", "用户名或密码错误!");
            RequestDispatcher requestdispatcher=req.getRequestDispatcher("TeacherLogin.jsp");
            requestdispatcher.forward(req, resp);
        }

    }
    private void rememberMe(String userName,String password,HttpServletResponse response){
        Cookie user;
        try {
            user = new Cookie("user", URLEncoder.encode(userName,"UTF-8")+"-"+URLEncoder.encode(password,"UTF-8"));
            user.setMaxAge(1*60*60*24*7);
            response.addCookie(user);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
